home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C25 / sumValue.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  757 b   |  28 lines

  1. //: C25:sumValue.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Sums the value of Trash in any type of STL
  7. // container of any specific type of Trash:
  8. #ifndef SUMVALUE_H
  9. #define SUMVALUE_H
  10. #include <typeinfo>
  11. #include <vector>
  12.  
  13. template<typename Cont>
  14. void sumValue(Cont& bin) {
  15.   double val = 0.0f;
  16.   typename Cont::iterator tally = bin.begin();
  17.   while(tally != bin.end()) {
  18.     val +=(*tally)->weight() * (*tally)->value();
  19.     out << "weight of "
  20.         << typeid(*(*tally)).name()
  21.         << " = " << (*tally)->weight() 
  22.         << endl;
  23.     tally++;
  24.   }
  25.   out << "Total value = " << val << endl;
  26. }
  27. #endif // SUMVALUE_H ///:~
  28.